﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.ServiceModel.DomainServices.Client;
using LEEAAttendanceControl.Web;

namespace LEEAAttendanceControl.Views
{
    public partial class Jobs : Page
    {
        public Jobs()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.AddEvent += NewJobHandler;
            myApp.EditEvent += EditJobHandler;
            myApp.DeleteEvent += DeleteJobHandler;
        }

        private void JobsDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        public void NewJobHandler(object sender, EventArgs e)
        {
            JobsEdit newJob = new JobsEdit("New Job");

            newJob.LoadData(0);
            newJob.Closed += EditClosed;

            newJob.Show();
        }

        public void EditJobHandler(object sender, EventArgs e)
        {
            Job editJob = JobsGrid.SelectedItem as Job;

            if (editJob != null)
            {
                JobsEdit newJob = new JobsEdit("Edit Job");

                newJob.LoadData(editJob.JobID);
                newJob.Closed += EditClosed;

                newJob.Show();
            }
        }

        public void DeleteJobHandler(object sender, EventArgs e)
        {
            if (JobsGrid.SelectedItem != null)
            {
                if (MessageBox.Show(ApplicationStrings.DeleteMessage, ApplicationStrings.Delete
                    , MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    JobsDataSource.DataView.Remove(JobsGrid.SelectedItem);

                    JobsDataSource.SubmitChanges();
                }
            }
        }

        protected void EditClosed(object sender, EventArgs e)
        {
            JobsDataSource.Load();
        }
    }
}
